package com.op.teachingliteapp.sender.handler;

import com.op.teachingliteapp.sender.listener.OnChannelStatusListener;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPromise;

import java.net.SocketAddress;

import android.util.Log;



/**
 * Created by Genji on 2015/5/10.
 */
public class ScreenSendHandler extends ChannelHandlerAdapter {
    String TAG = this.getClass().getSimpleName();
    private OnChannelStatusListener onChannelStatusListener;

    public void setOnChannelStatusListener(OnChannelStatusListener onChannelStatusListener) {
        this.onChannelStatusListener = onChannelStatusListener;
    }


    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        Log.i("channelActive", "----------defult-----------");
        this.onChannelStatusListener.channelActive(ctx);

    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        Log.i(TAG, "channelInactive");
        this.onChannelStatusListener.channelInactive(ctx);
        super.channelInactive(ctx);
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        Log.i(TAG, "channelRead");
        this.onChannelStatusListener.channelRead(ctx, msg);

        super.channelRead(ctx, msg);
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        Log.i(TAG, "channelReadComplete");
        this.onChannelStatusListener.channelReadComplete(ctx);
        super.channelReadComplete(ctx);
    }

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        Log.i(TAG, "userEventTriggered");
        this.onChannelStatusListener.userEventTriggered(ctx,evt);
        super.userEventTriggered(ctx, evt);
    }

    @Override
    public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
        super.channelWritabilityChanged(ctx);
    }

    @Override
    public void bind(ChannelHandlerContext ctx, SocketAddress localAddress, ChannelPromise promise) throws Exception {
        super.bind(ctx, localAddress, promise);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        Log.i(TAG, "exceptionCaught");
        this.onChannelStatusListener.exceptionCaught(ctx,cause);
        cause.printStackTrace();
        ctx.close();

    }


}
